<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>添加</title>
    <#assign base=request.contextPath  />
    <base id="base" href="${base}">
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, adminUser-scalable=0">
    <link rel="stylesheet" href="${base}/static/win10/component/layui/css/layui.css" media="all">
    <script>
        var base =  "${base}";
    </script>
</head>
<body>
<br>
<form class="layui-form " action="">
        <input type="hidden" id="industryId" name="industryId" value="${wechatIndustry.industryId!}" >
        <div class="layui-row">
            <div class="layui-col-xs6">
                <fieldset class="layui-elem-field layui-field-title">
                    <legend>所有行业</legend>
                </fieldset>
                <div id="test9" class="demo-tree demo-tree-box" style="width: 80%; height: 450px;"></div>
            </div>
            <div class="layui-col-xs6">
                <fieldset class="layui-elem-field layui-field-title">
                    <legend>行业添加</legend>
                </fieldset>
                <div class="layui-form-item">
                    <label class="layui-form-label">行业名字</label>
                    <div class="layui-input-block">
                        <input type="text" id="industryName" name="industryName" value="${wechatIndustry.industryName!}"   autocomplete="off" placeholder="请输入行业名字"
                               class="layui-input">
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">选择上级</label>
                    <div class="layui-input-block">
                        <input type="hidden" name="parentId" id="parentId" value="${wechatIndustry.parentId!}">
                        <input type="text" id="parentName" class="layui-input" value="${parentName!}" readonly placeholder="请选择上级">
                    </div>
                </div>
            </div>
        </div>

    <div align="center">
        <button type="button" class="layui-btn" id="submit">保存</button>
    </div>
</form>

<script src="${base}/static/win10/component/layui/layui.js?t=1"></script>
<script src="${base}/static/win10/js/jquery-1.8.3.min.js?t=1"></script>
<script>
    layui.use(['tree','form', 'layer'], function() {
        var form = layui.form,tree = layui.tree
                , layer = layui.layer;
        //获取树形结构数据
        var data1= getTreeData();
        //渲染树形结构
        tree.render({
            elem: '#test9'
            ,data: data1
            ,click: function(obj){
                //layer.msg(JSON.stringify(obj.data));
                //选中节点高亮
                var nodes = document.getElementsByClassName("layui-tree-txt");
                for(var i=0;i<nodes.length;i++){
                    if(nodes[i].innerHTML === obj.data.title)
                        nodes[i].style.color = "red";
                    else
                        nodes[i].style.color= "#555";
                }
                    //赋值
                    $("#parentId").val(obj.data.id);
                    $("#parentName").val(obj.data.title);
            }
        });
        //回显默认选中
        var nodes = document.getElementsByClassName("layui-tree-txt");
        var pName = $("#parentName").val();
        //console.log(nodes[0].innerHTML+"==="+pName)
        for (let i = 0; i < nodes.length; i++) {
            if(nodes[i].innerHTML === pName)
                nodes[i].style.color = "red";
            else
                nodes[i].style.color= "#555";
        }

        $("#submit").on("click",function () {
            if(check()===false){
                return;
            }

            var formDate = {
                         industryId:$("#industryId").val(),
                         industryName:$("#industryName").val(),
                           parentId:$("#parentId").val()
            }
            $.ajax({
                type: "POST",
                url: base+"/WeChatUser/wechatIndustry/admin/editWechatIndustry",
                data: formDate,
                success: function (data) {
                    layer.msg(data.message)
                    if (data.code === 200) {
                        window.location.href=base+"/WeChatUser/wechatIndustry/admin/main.html"
                    } else {

                    }
                }
            });
        })

        function check(){
                   if($("#industryName").val().length<1){
                       layer.msg("请输入行业名字");
                       return false;
                   }
                   if($("#parentId").val().length<1){
                       layer.msg("请输入上级id");
                       return false;
                   }
        }
        //加载树形插件数据
        function getTreeData() {
            var dt;
            $.ajax({
                type: "POST",
                async: false,
                url: base+"/WeChatUser/wechatIndustry/admin/getTree",
                success: function (data) {
                    dt = data.data;
                }
            });
            return dt;
        }


    });

</script>
</body>
</html>